package cn.dnui_lxx432.dnuifood_16110100432.model;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.widget.Toast;

import java.util.List;

import cn.dnui_lxx432.dnuifood_16110100432.JavaBean.CollectResultBean;
import cn.dnui_lxx432.dnuifood_16110100432.JavaBean.CollectStatus;
import cn.dnui_lxx432.dnuifood_16110100432.R;
import cn.dnui_lxx432.dnuifood_16110100432.controller.BaseActivity;
import cn.dnui_lxx432.dnuifood_16110100432.controller.FoodListActivity;
import cn.dnui_lxx432.dnuifood_16110100432.listener.OnRetrofitListener;
import cn.dnui_lxx432.dnuifood_16110100432.service.CollectService;
import cn.dnui_lxx432.dnuifood_16110100432.service.ShopService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class CollectModel extends AppCompatActivity {
    private Context context;
    private BaseActivity baseActivity;
    public CollectModel(){


    }

    public void isCollect(String user_id, String shop_food_id, String flag, final Menu menu){

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175:8080/foodService/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        CollectService collectService = retrofit.create(CollectService.class);
        Call<CollectStatus> call = collectService.isCollected(user_id,shop_food_id,flag);
        Callback<CollectStatus> callback = new Callback<CollectStatus>() {
            @Override
            public void onResponse(Call<CollectStatus> call, Response<CollectStatus> response) {

                if (response.body().getCollected().equals("1")){
                    menu.findItem(R.id.item_collect).setTitle("已收藏");

                }else {
                    menu.findItem(R.id.item_collect).setTitle("收藏");


                }
            }

            @Override
            public void onFailure(Call<CollectStatus> call, Throwable t) {
                Toast.makeText(CollectModel.this,"网络连接失败",Toast.LENGTH_SHORT).show();

            }
        };
        call.enqueue(callback);
    }

    public void userCollectShop(String user_id, String shop_id, final Menu menu){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175:8080/foodService/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        CollectService collectService = retrofit.create(CollectService.class);
        Call<CollectStatus> call = collectService.userCollectShop(user_id,shop_id);
        Callback<CollectStatus> callback = new Callback<CollectStatus>() {
            @Override
            public void onResponse(Call<CollectStatus> call, Response<CollectStatus> response) {

                if (response.body().getSuccess().equals("1")){
                    menu.findItem(R.id.item_collect).setTitle("已收藏");


                }else {
                    Toast.makeText(CollectModel.this, "收藏店铺失败", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<CollectStatus> call, Throwable t) {

                Toast.makeText(context, "网络连接失败", Toast.LENGTH_SHORT).show();
            }
        };
        call.enqueue(callback);

    }

    public void unCollectShop(String user_id, final String shop_id, final List<CollectStatus> mDataList){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175:8080/foodService/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        CollectService collectService = retrofit.create(CollectService.class);
        Call<CollectStatus> call = collectService.userCollectShop(user_id,shop_id);
        Callback<CollectStatus> callback = new Callback<CollectStatus>() {
            @Override
            public void onResponse(Call<CollectStatus> call, Response<CollectStatus> response) {
                if (response.body().getSuccess().equals("1")){


                }else {

                }

            }


            @Override
            public void onFailure(Call<CollectStatus> call, Throwable t) {
                baseActivity.showToast("网络连接失败");
            }
        };
        call.enqueue(callback);

    }
    public void unCollectShop_01(String user_id, String shop_id, final Menu menu){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175:8080/foodService/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        CollectService collectService = retrofit.create(CollectService.class);
        Call<CollectStatus> call = collectService.userCollectShop(user_id,shop_id);
        Callback<CollectStatus> callback = new Callback<CollectStatus>() {
            @Override
            public void onResponse(Call<CollectStatus> call, Response<CollectStatus> response) {
                if (response.body().getSuccess().equals("1")){
                    menu.findItem(R.id.item_collect).setTitle("收藏");


                }else {

                }

            }


            @Override
            public void onFailure(Call<CollectStatus> call, Throwable t) {
                Toast.makeText(context, "网络连接失败", Toast.LENGTH_SHORT).show();
            }
        };

        call.enqueue(callback);
    }

    public void unCollectFood(String user_id, final String food_id, final List<CollectStatus> mDataList){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175:8080/foodService/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        CollectService collectService = retrofit.create(CollectService.class);
        Call<CollectStatus> call = collectService.userCollectFood(user_id,food_id);
        Callback<CollectStatus> callback = new Callback<CollectStatus>() {
            @Override
            public void onResponse(Call<CollectStatus> call, Response<CollectStatus> response) {
                if (response.body().getSuccess().equals("1")){

                }else {
                }

            }


            @Override
            public void onFailure(Call<CollectStatus> call, Throwable t) {
                Toast.makeText(context, "网络连接失败", Toast.LENGTH_SHORT).show();
            }
        };
        call.enqueue(callback);

    }
    public void unCollectFood_01(String user_id, final String food_id, final Menu menu){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175:8080/foodService/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        CollectService collectService = retrofit.create(CollectService.class);
        Call<CollectStatus> call = collectService.userCollectFood(user_id,food_id);
        Callback<CollectStatus> callback = new Callback<CollectStatus>() {
            @Override
            public void onResponse(Call<CollectStatus> call, Response<CollectStatus> response) {
                if (response.body().getSuccess().equals("1")){
                    menu.findItem(R.id.item_collect).setTitle("收藏");
                }else {

                }

            }
            @Override
            public void onFailure(Call<CollectStatus> call, Throwable t) {
                Toast.makeText(context, "网络连接失败", Toast.LENGTH_SHORT).show();
            }
        };
        call.enqueue(callback);

    }

    public void userCollectFood(String user_id, String food_id, final Menu menu){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175:8080/foodService/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        CollectService collectService = retrofit.create(CollectService.class);
        Call<CollectStatus> call = collectService.userCollectFood(user_id,food_id);
        Callback<CollectStatus> callback = new Callback<CollectStatus>() {
            @Override
            public void onResponse(Call<CollectStatus> call, Response<CollectStatus> response) {
                if (response.body().getSuccess().equals("1")){
                    menu.findItem(R.id.item_collect).setTitle("已收藏");

                }else {
                    Toast.makeText(CollectModel.this, "收藏店铺失败", Toast.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onFailure(Call<CollectStatus> call, Throwable t) {

            }
        };

        call.enqueue(callback);
    }


    public void getAllUserCollection(String user_id,String flag,final OnRetrofitListener listener){

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175:8080/foodService/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        CollectService collectService = retrofit.create(CollectService.class);
        Call<List<CollectStatus>> call = collectService.getAllUserCollection(user_id,flag);
        Callback<List<CollectStatus>> callback = new Callback<List<CollectStatus>>() {
            @Override
            public void onResponse(Call<List<CollectStatus>> call, Response<List<CollectStatus>> response) {

                listener.onSuccess(response.body(),8);

            }
            @Override
            public void onFailure(Call<List<CollectStatus>> call, Throwable t) {
                Toast.makeText(context,"网络连接失败",Toast.LENGTH_SHORT).show();
                listener.onFailure("失败");
            }
        };
        call.enqueue(callback);
    }
}
